package com.nettest.action;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "json-default")
@Results({ @Result(name = "success", type = "json", params = {
		"ignoreHierarchy", "false" }) })
public abstract class BaseJsonAction extends ActionSupport {

	private static final long serialVersionUID = 5104043936930382354L;

	private Collection<String> actionMessages = new ArrayList<String>();
	private boolean success;
	private int total;
	private String sort;
	private String order;	
	private int page;
	private int rows;
	
	public void setActionMessages(Collection<String> messages) {
		actionMessages = messages;
	}

	public Collection<String> getActionMessages() {
		return actionMessages;
	}

	public void addActionMessage(String aMessage) {
		actionMessages.clear();
		actionMessages.add(aMessage);
	}

	public void setFieldErrors(String fieldErrors) {
	}

	public boolean isNotEmpty(Object value) {
		return !isEmpty(value);
	}

	public boolean isEmpty(Object value) {
		if (value == null || "".equals(value)) {
			return true;
		}

		if (value instanceof String) {
			String str = (String) value;
			if ("null".equals(str)) {
				return true;
			}
		}
		return false;
	}
	
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected String getParameter(String paramName) {
		return this.getRequest().getParameter(paramName);
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	@JSON(name="pageSize")
	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
	
	public int getFirstResult() {
		int startNo = (page - 1) * rows;
		return startNo;
	}
	public int getMaxResults() {
		return getFirstResult() + rows;
	}
}
